/* Copyright (c) 2004-2022 peigen.info. All rights reserved. */

package info.peigen.hotpot.common.core.enums;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <b>(CertType)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/9/14
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum CertType implements Messageable {
    /** 为了对抗 Alibaba Java Coding 而存在的注释 */
    Identity_Card("Id", "身份证", "00"),
    Army_Identity_Card("Army_Id", "军官证", "01"),
    Passport("Passport", "护照", "01"),
    Home_Return_Permit("Home_Return", "回乡证", "04"),
    Taiwan_Compatriot_Entry_Permit("Taiwan", "台胞证", ""),
    Officers_Card("Officers_Card", "警官证", ""),
    Soldiers_Card("Soldiers_Card", "士兵证", "03"),
    Business_License("Business_License", "营业执照", "62"),
    Organization_Code_Certificate("Organization_Code_Certificate", "组织机构代码证", "61"),
    Tax_Registration_Certificate("Tax_Registration_Certificate", "税务登记证", "63"),
    Uniform_Social_Credit_Code("Uniform_Social_Credit_Code", "统一社会信用代码", "66"),
    Institution_Legal_Person_Certificate("ILPC", "事业单位法人证书", ""),
    Household_Register("Household_Register", "户口簿", "05"),
    HK_Macao_Pass("HK_Macao_Pass", "港澳通行证", ""),
    HK_Id("HK_Id", "香港本地身份证", ""),
    Macao_Id("Macao_Id", "澳门本地身份证", ""),
    Taiwan_id("Taiwan_id", "台湾本地身份证", ""),
    Alien_Residential_Certificate("ARC", "外国人居留证", "06"),
    Other_Personal("Other_Personal", "其它个人证件类型", "07"),
    Other_Corporate("Other_Corporate", "其它机构证件", "69"),
    /** 不晓得啷格回事 */
    UNKNOWN("--", "不晓得啷格回事", "");

    /** 枚举值 */
    String code;

    /** 枚举描述 */
    String message;

    /** 汇付天下 代码 */
    String huifuCode;

    /**
     * 通过枚举<code>code</code>获得枚举
     *
     * @param code 枚举值
     *
     * @return CertType
     */

    public static CertType getByCode(String code) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equals(code, innerEnum.getCode()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 通过枚举<code>name</code>获得枚举
     *
     * @param name 枚举名
     *
     * @return CertType
     */
    public static CertType getByName(String name) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equalsIgnoreCase(name, innerEnum.name()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 获取全部枚举
     *
     * @return List<CertType>
     */
    public static List<CertType> getAllEnum() {
        return Lists.newArrayList(values());
    }

    /**
     * 获取全部枚举值
     *
     * @return List<String>
     */
    public static List<String> getAllEnumCode() {
        return getAllEnum().stream().map(CertType::code).collect(Collectors.toList());
    }

    /**
     * 通过code获取msg
     *
     * @param code 枚举值
     *
     * @return String
     */
    public static String getMsgByCode(String code) {
        return Optional.ofNullable(code).map(
                str ->
                        Optional.ofNullable(getByCode(str))
                                .map(CertType::message).orElse(UNKNOWN.message())
        ).orElse(null);
    }

    /**
     * 获取枚举code
     *
     * @param innerEnum 枚举
     *
     * @return code
     */
    public static String getCode(CertType innerEnum) {
        return Optional.ofNullable(innerEnum).map(CertType::code).orElse(UNKNOWN.code());
    }

    /**
     * map
     *
     * @return Map
     */
    public static Map<String, String> maps() {
        return Stream.of(values()).collect(Collectors.toMap(CertType::code, CertType::message));
    }

    /**
     * 判断给定的枚举，是否在列表中
     *
     * @param values 列表
     *
     * @return boolean
     */
    public boolean isInList(CertType... values) {
        for (CertType e : values) {
            if (this == e) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return Returns the code.
     */
    @Override
    public String code() {
        return code;
    }

    /**
     * @return Returns the message.
     */
    @Override
    public String message() {
        return message;
    }
}